package com.dailyblue.java.io;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

// 字节流--->字节输入流--->ByteArrayInputStream
public class DemoC {

    private static void a() throws IOException {
        String str = "Hello,world!";
        byte[] bytes = str.getBytes();
        // 产生一个读取字节数组的一个流对象
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        int content;
        // 字节ASCII码 如果不存在返回-1
        while ((content = bais.read()) != -1) {
            // int--->char
            char ch = (char) content;
            //System.out.println(content);
            System.out.print(ch);
        }
        bais.close();
    }

    private static void b() throws IOException {
        String str = "0123456789";
        byte[] bytes = str.getBytes();
        InputStream in = new ByteArrayInputStream(bytes);
        byte[] buf = new byte[4];
        // len 代表实际读取的长度 读取的内容存放到了buf数组
        int len;
        while ((len = in.read(buf)) != -1) {
            String s = new String(buf,0,len);
            System.out.print(s);
        }
        in.close();
    }

    public static void main(String[] args) throws IOException {
        b();
    }
}
